package pathways;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.util.Vector;

public class CSubstratePainter implements ISubstratePainter
{
  public void draw(Graphics g, CSubstrate s)
  {
    if (s.isVisible())
    {
      Rectangle rect = s.getRect();
      CCoOrdinates coords = s.getCoOrdinates();

      g.fillRect(rect.x,rect.y,rect.width,rect.height);

      highLightMoreThanOneBranchAreas(g,s);

      drawText(s.getName(),coords.getPoint0(),coords.getPoint2(),Color.white,g);
    }
  }

  private void highLightMoreThanOneBranchAreas(Graphics g, CSubstrate s)
  {
    Rectangle r = s.getRect();

    Vector rxns = s.getConnectionPoint0().getFirstReactionAtEachBranch();
    Point b1 = null;
    Point b2 = null;
    Point peak = new Point(r.x+r.width/2,r.y+r.height/2);

    Color c = g.getColor();
    g.setColor(Color.blue);

    if (rxns.size() > 1)
    {
      // color it
      b1 = new Point(r.x,r.y);
      b2 = new Point(r.x,r.y + r.height);

      g.fillPolygon(getPolygon(b1,b2,peak));
    }

    rxns = s.getConnectionPoint1().getFirstReactionAtEachBranch();

    if (rxns.size() > 1)
    {
      // color it
      b1 = new Point(r.x,r.y);
      b2 = new Point(r.x+r.width,r.y);

      g.fillPolygon(getPolygon(b1,b2,peak));
    }

    rxns = s.getConnectionPoint2().getFirstReactionAtEachBranch();

    if (rxns.size() > 1)
    {
      // color it
      b1 = new Point(r.x+r.width,r.y);
      b2 = new Point(r.x+r.width,r.y+r.height);

      g.fillPolygon(getPolygon(b1,b2,peak));
    }

    rxns = s.getConnectionPoint3().getFirstReactionAtEachBranch();

    if (rxns.size() > 1)
    {
      // color it
      b1 = new Point(r.x,r.y+r.height);
      b2 = new Point(r.x+r.width,r.y+r.height);

      g.fillPolygon(getPolygon(b1,b2,peak));
    }

    g.setColor(c);
  }

  private Polygon getPolygon(Point b1, Point b2, Point peak)
  {
    int x[] = new int[3];
    int y[] = new int[3];

    x[0] = b1.x;
    x[1] = b2.x;
    x[2] = peak.x;

    y[0] = b1.y;
    y[1] = b2.y;
    y[2] = peak.y;

    return (new Polygon(x,y,3));
  }
  private void drawText(String text, Point source, Point dest, Color color, Graphics g)
  {
    Font f = new Font("Verdana",0,10);
    FontMetrics fm = g.getFontMetrics(f);
    int charWidth = fm.charWidth('Z');

    double distance = source.distance(dest);
    int numOfCharsPrintable = (int)(distance/charWidth);

    String textToPrint = new String("");

    int extraPrintable = 0;
    // we will use this to offset the x of where we print the
    // string because we cannot just add spaces
    // because spaces are truncated by g.drawString()
    int extraPrintableOnEitherSide = 0;

    // if we can print all of it then do so else
    // print whatever we can with "..."
    // atleast print "..."
    if (text.length() <= numOfCharsPrintable)
    {
      textToPrint =  text;

      extraPrintable = numOfCharsPrintable - text.length();
      extraPrintableOnEitherSide = extraPrintable/2;
    }
    else
    {
      if (numOfCharsPrintable > 3)
         textToPrint = text.substring(0,numOfCharsPrintable-3);

      textToPrint = textToPrint + "...";
    }

    Font oldfont = g.getFont();
    Color c = g.getColor();
    g.setColor(color);
    g.setFont(f);

    g.drawString(textToPrint,source.x + charWidth * extraPrintableOnEitherSide, source.y);

    g.setColor(c);
    g.setFont(oldfont);
  }
}